package com.cjx.leetcode.backtracking;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Objects;

/**
 * @author cjx
 * @Description E17.电话号码的字母组合【中】
 * @date 2024/3/31 9:58
 **/
public class E17 {

    List<String> result = new ArrayList<>();
    StringBuilder builder = new StringBuilder();

    public List<String> letterCombinations(String digits) {


        char[][] mapping = new char[][] {
                {'a', 'b', 'c'},
                {'d', 'e', 'f'},
                {'g', 'h', 'i'},
                {'j', 'k', 'l'},
                {'m', 'n', 'o'},
                {'p', 'q', 'r', 's'},
                {'t', 'u', 'v'},
                {'w', 'x', 'y', 'z'},
        };
        int length = digits.length();
        char[][] characterTable = new char[length][];
        char[] charArray = digits.toCharArray();

        for (int i = 0; i < length; i++) {
            int digit = charArray[i] - '2';
            characterTable[i] = mapping[digit];
        }
        backtracking(characterTable,length,0);
        if (Objects.equals(result.get(0), "")){
            result.clear();
            return result;
        }
        return result;
    }

    public void backtracking(char[][] charArray,int length,int startIndex){
        if (builder.length() == length){
            result.add(String.valueOf(builder));
            return;
        }
        for (int i = startIndex,j = 0; i < length && j <charArray[i].length ; j++) {
            builder.append(charArray[i][j]);
            backtracking(charArray,length,i+1);
            builder.deleteCharAt(builder.length()-1);
        }
    }

    public static void main(String[] args) {
        System.out.println(new E17().letterCombinations("23"));
    }

}
